<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'"
               :close-on-click-modal="false"
               :visible.sync="visible"
               width="500px"
    >
        <el-form :model="dataForm"
                 :rules="dataRule"
                 ref="dataForm"
                 @keyup.enter.native="dataFormSubmit()"
                 label-width="110px">
            <el-form-item label="行业代码" prop="industryCode">
                <el-input v-model="dataForm.industryCode"></el-input>
            </el-form-item>
            <el-form-item label="行业名称" prop="industryName">
                <el-input v-model="dataForm.industryName"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="industryBiz">
                <el-input v-model="dataForm.industryBiz"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer"
              class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
import { Debounce } from '@/utils/debounce'
import {custTypeList,departList} from '@/utils/data'
export default {
	data () {
		return {
			visible: false,
            custTypeList: custTypeList,
            departList: departList,
            provinceList: [],
            cityList: [],
            areaList: [],
			roleList: [],
            personList: [],
			dataForm: {
                industryCode: "",
                industryName: "",
                industryBiz: "",
                id: ""
			},
			dataRule: {
                industryCode: [
                    { required: true, message: '行业代码不能为空', trigger: 'blur' }
                ],
                industryName: [
                    { required: true, message: '行业名称不能为空', trigger: 'blur' }
                ],
			}
		}
	},
	methods: {
		init (id) {
            console.log('🚀 ~ id', id)
			this.dataForm.id = id || 0
			this.visible = true
            console.log('🚀 ~ this.dataForm', this.dataForm)
            if(this.dataForm.id){
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        this.$http({
                            url: this.$http.adornUrl('/crm/base/cusIndustry/info/' + this.dataForm.id),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({ data }) => {
                            this.dataForm = data
                        })
                    }
                })
            }else{
                this.dataForm = {
                    industryCode: "",
                    industryName: "",
                    industryBiz: "",
                    id: ""
                }
            }
		},
  
		// 表单提交
		dataFormSubmit: Debounce(function () {
			this.$refs['dataForm'].validate((valid) => {
				if (valid) {
					this.$http({
						url: this.$http.adornUrl( this.dataForm.id ? '/crm/base/cusIndustry/updCustIndustry' : '/crm/base/cusIndustry/saveCustIndustry'),
						method: this.dataForm.id ? 'put' : 'post',
						data: this.$http.adornData({
                            ...this.dataForm,
                            id: this.dataForm.id ? this.dataForm.id : wfy.uuid(32)
                        })
					}).then(({ data }) => {
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.visible = false
								this.$emit('refreshDataList')
							}
						})
					})
				}
			})
		})
	}
}
</script>
